//
//  StatusViewModel.swift
//  SinaWeiBo
//
//  Created by ZKJ on 2017/9/11.
//  Copyright © 2017年 ZKJ. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    // MARK:- 定义属性
    var status : StatusModel?
    var cellHeight : CGFloat = 0
    
    // MARK:- 对数据处理的属性
    var sourceText : String?            //处理来源
    var createText : String?            //处理创建时间
    var verifiedImage : UIImage?        //处理用户认证图标
    var vipImage : UIImage?             //处理用户会员等级
    var profileURL : URL?               //处理用户头像的地址
    var picUrls : [URL] = [URL]()       //处理微博配图的数据
    
    
    // MARK:- 自定义构造函数
    init(status : StatusModel) {
        self.status = status
        
        // 1.微博来源处理
        guard let source = status.source, source != "" else {
            return
        }
        let start = (source as NSString).range(of: ">").location + 1
        let length = (source as NSString).range(of: "</").location - start
        sourceText = (source as NSString).substring(with: NSRange(location: start, length: length))
        
        // 2.对时间处理
        guard let created_at = status.created_at else {
            return
        }
        createText = Date.createDateString(creatDate: created_at)
        
        //3.用户的认证类型
        let verified_type = status.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        
        //4.用户的会员等级
        let mbrank = status.user?.mbrank ?? 0
        if mbrank > 0 && mbrank <= 6 {
            vipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        //5.处理用户头像的地址
        let profileUrlString = status.user?.profile_image_url ?? ""
        profileURL = URL(string: profileUrlString)
        
        //6.处理微博配图的数据
        let picUrlArr = status.pic_urls!.count != 0 ? status.pic_urls : status.retweetedstatus?.pic_urls
        if let picUrlStrArr = picUrlArr {
            for picDic in picUrlStrArr {
                guard let picUrlStr = picDic["thumbnail_pic"] else {
                    continue
                }
                picUrls.append(URL(string: picUrlStr)!)
            }
        }
    }
}



